﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yunchang;

namespace AutoChess
{
    public class CellShipLoadHelper
    {
        private Transform _modelTransform;
        private GameObject _modelObj;
        private Action _onModelLoadComplete;
        private LoadEventManager _loadEventManager;
        private SailorAnimation _anim;
        private string _loadEventName;
        private int _shipId;
        private int _curShipModelId;
        private int _defaltLayer = 0;

        private Vector3 _localPositionOffset;
        private Vector3 _localScale = Vector3.one;
        private Vector3 _localEulerAngles = new Vector3(0, 45, 0);
        public CellShipLoadHelper(Transform modelTransform, LoadEventManager loadEventManager, string loadEventName,Action onLoadComplete)
        {
            _curShipModelId = 0;
            _loadEventManager = loadEventManager;
            _onModelLoadComplete = onLoadComplete;
            _modelTransform = modelTransform;
            _loadEventName = loadEventName;
            _defaltLayer = modelTransform.gameObject.layer;
        }

        public void SetData(int shipId)
        {
            _shipId = shipId;
        }
        public void SetModelOffset(Vector3 localPosition, Vector3 localScale, Vector3 localAngle)
        {
            this._localPositionOffset = localPosition;
            this._localEulerAngles = localAngle;
            this._localScale = localScale;
            if (_modelObj != null)
            {
                _modelObj.transform.localPosition = _localPositionOffset;
                _modelObj.transform.localScale = _localScale;
                _modelObj.transform.localEulerAngles = _localEulerAngles;
            }
        }
        public void LoadModel()
        {
            int modelId = 0;
            var dictModel = Table.Blo.TableShipBlo.GetTableShip(_shipId);
            if (dictModel != null)
            {
                //设置模型偏移等默认值
                var temp = Table.Blo.TableShipBlo.GetTableShipModel(dictModel.modelId);
                if (temp != null)
                {
                    modelId = temp.ModelId;
                    _localScale = Vector3.one * 0.35f;
                }
            }
            if (_curShipModelId == modelId)
            {
                return;
            }
            UnloadModel();
            if (modelId > 0)
            {
                _loadEventManager.SendEvent(LoadEventType.MODEL, modelId, _loadEventName);
                _curShipModelId = modelId;
            }
        }

        public void UnloadModel()
        {
            var e = _loadEventManager.GetLoadEvent(LoadEventType.MODEL, _curShipModelId, _loadEventName) as ModelLoadEvent;
            if (e != null)
            {
                if (_modelObj != null)
                {
                    var capsuleCollider = _modelObj.GetComponent<CapsuleCollider>();
                    if (capsuleCollider != null)
                    {
                        GameObject.Destroy(capsuleCollider);
                    }
                    var kit = _modelObj.GetComponent<ModelRenderKit>();
                    if (kit != null)
                    {
                        kit.UnInitialize();
                    }
                    e.UnSpawnModel(_modelObj);
                }
                _modelObj = null;
                e.Unload();
                _curShipModelId = 0;
            }
        }

        public void UpdateModel(int modelId, GameObject modelObj)
        {
            _curShipModelId = modelId;//真正的模型Id
            _modelObj = modelObj;
        }

        public void Update()
        {
            if (_curShipModelId > 0 && _modelObj == null)
            {
                var state = _loadEventManager.GetEventStatus(LoadEventType.MODEL, _curShipModelId, _loadEventName);
                if (state == LoadEventBase.LoadEventState.Loaded)
                {
                    OnModelLoadComplete();
                }
            }
        }

        public void ResetModelLayer(string layerName)
        {
            _defaltLayer = LayerMask.NameToLayer(layerName);
            if (_modelObj != null)
            {
                CommonMethod.ChangeLayer(_modelObj.transform, _defaltLayer);
            }
        }
        private void OnModelLoadComplete()
        {
            var e = _loadEventManager.GetLoadEvent(LoadEventType.MODEL, _curShipModelId, _loadEventName) as ModelLoadEvent;
            if (e != null)
            {
                _modelObj = e.SpawnModel();
                if (_modelObj != null)
                {
                    CommonMethod.ChangeLayer(_modelObj.transform, _defaltLayer);

                    _modelObj.transform.SetParent(_modelTransform);
                    _modelObj.transform.localPosition = _localPositionOffset;
                    _modelObj.transform.localScale = _localScale;
                    _modelObj.transform.localEulerAngles = _localEulerAngles;
                    _modelObj.name = _curShipModelId.ToString();

                    var kit = _modelObj.GetComponent<ModelRenderKit>();
                    if (kit != null)
                    {
                        kit.Initialize();
                    }

                    var capsuleCollider = _modelObj.AddMissingComponent<CapsuleCollider>();
                    capsuleCollider.center = new Vector3(0, 0.8f, 0);
                    capsuleCollider.radius = 0.3f;
                    capsuleCollider.height = 1.8f;
                    _anim = _modelObj.GetComponent<SailorAnimation>();
                }
            }
            if (_onModelLoadComplete != null)
                _onModelLoadComplete();

        }

        public void PlayAnim(String animName)
        {
            if (null != _anim)
            {
                _anim.Play(animName);
            }
        }
    }

}
